<?php

class Auth
{
	 public static function isLogged()
	 {
		 if (isset($_SESSION['user_id']))
		 {
			 return $_SESSION['user_id'];
		 }
		 else
		 {
			 return false;
		 }
	 }
	 
	 public static function login($email, $password)
	 {
		 $password=md5($password);
		 $data=SQL::select(Template::render("getUserID.sql", "sql", array('email'=>$email,'password'=>$password)));
		 
		 if ($data)
		 {
			 $_SESSION['user_id']=$data[0]['id'];
			 setcookie("user_email", $email, time()+60*60*24*30 , "/");
			 setcookie("password", $password, time()+60*60*24*30 , "/");
			 return true;
		 }
		 else
		 {
			 return false;
		 }
	 } 
	 
	 public static function isCheckSuccess()
	 {
		 if (self::isLogged())
		 {
			 return true;
		 }
		 else
		 {
		     if (isset($_COOKIE["password"])&& isset($_COOKIE["user_email"])) 
			{
				 $password=safe_var($_COOKIE['password']);
				 $email=safe_var($_COOKIE['user_email']);
				 
				 $data=SQL::select(Template::render("getUserID.sql", "sql", array('email'=>$email,'password'=>$password)));

				 if ($data)
				 {			 
					 $_SESSION['user_id']=$data[0]['id'];
					 $_SESSION['user_name']=$data[0]['user_name'];
					 return true;
				 }
				 else
				 {
					 return false;
				 }
			}
			else
			{
				 return false;
			}
		}	
	 }
}